<template>
  <template v-if="hasChildren">
    <el-sub-menu :index="route.path">
      <template #title>
        <el-icon v-if="route.meta?.icon">
          <component :is="icons[route.meta.icon]" />
        </el-icon>
        <span>{{ route.meta?.title }}</span>
      </template>
      <recursive-menu
        v-for="child in route.children"
        :key="child.path"
        :route="child"
        :icons="icons"
      />
    </el-sub-menu>
  </template>
  <template v-else>
    <el-menu-item :index="getMenuItemIndex(route)">
      <el-icon v-if="route.meta?.icon">
        <component :is="icons[route.meta.icon]" />
      </el-icon>
      <template #title>{{ route.meta?.title }}</template>
    </el-menu-item>
  </template>
</template>

<script lang="ts" setup>
import { computed } from "vue";

const props = defineProps<{
  route: any;
  icons: Record<string, any>;
}>();

const hasChildren = computed(() => {
  return props.route.children && props.route.children.length > 0;
});

const getMenuItemIndex = (route: any) => {
  if (route.meta?.query) {
    // 生成带查询参数的完整路径
    const query = new URLSearchParams(route.meta.query).toString();
    return `${route.path}?${query}`;
  }
  return route.path;
};
</script>
